package com.hh.legalStoreRoom.service.impl;

import com.hh.legalFactory.mapper.LegalPersonMapper;
import com.hh.legalStoreRoom.VO.*;
import com.hh.legalStoreRoom.mapper.LegalPersonRoomMapper;
import com.hh.legalStoreRoom.service.LegalStoreRoomService;
import com.hh.store.VO.CommonGetStoreVo;
import com.hh.store.VO.StoreAreaVo;
import com.hh.store.VO.StoreRoomVo;
import com.hh.store.mapper.StoreMapper;
import com.hh.storeRoom.bean.StoreRoom;
import com.hh.utils.PageVo;
import com.hh.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;


import javax.annotation.Resource;
import java.util.List;


/**
 * @author Hanh
 * @date 2023/8/16 15:59s
 */
@Service
public class LegalStoreRoomImpl implements LegalStoreRoomService {

    @Resource
    private LegalPersonRoomMapper legalPersonRoomMapper;

    @Resource
    private LegalPersonMapper legalPersonMapper;

    @Autowired
    private StoreMapper storeMapper;

    /**
     * 分页查询
     * @param queryStorePageVo
     * @return
     */
    @Override
    public ResultVo queryStorePage(QueryLegalStoreRoomPageVo queryStorePageVo) {
        //计算总条数
        //1.计算总的数据量
        Integer totalNumber = legalPersonRoomMapper.getLegalPersonRoomCount(queryStorePageVo);
        //2.计算偏移量
        queryStorePageVo.setOffsize((queryStorePageVo.getPageIndex()-1)* queryStorePageVo.getPageSize());
        //3.获取合适条件的数据
        List<LegalRoomForQueryWebVo> storeList = legalPersonRoomMapper.getlegalPersonRoomByQuery(queryStorePageVo);
        //4.计算总页数
        PageVo<LegalRoomForQueryWebVo> pageVo = new PageVo<>();
        Integer totalPage =totalNumber%queryStorePageVo.getPageSize()==0?totalNumber/queryStorePageVo.getPageSize():
                totalNumber/queryStorePageVo.getPageSize()+1;
        pageVo.setTotalNumber(totalNumber);
        pageVo.setPageIndex(queryStorePageVo.getPageIndex());
        pageVo.setDataList(storeList);
        pageVo.setOffSize((queryStorePageVo.getPageIndex()- 1)* queryStorePageVo.getPageSize());
        pageVo.setTotalPage(totalPage);
        return ResultVo.success(pageVo);
    }

    /**
     * 添加
     * @param addLegalStoreVo
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public ResultVo addLegalStoreRoom(AddLegalStoreVo addLegalStoreVo) {
        //获取该法人原有的库位code信息
        List<String> storeCodes = legalPersonRoomMapper.getStoreByLegalCode(addLegalStoreVo.getLegalPersonCode());
        //如果原法人下没有库位code
        try {
            //如果原有数据为空，现有数据为空的情况下
            if (storeCodes.size()==0&&addLegalStoreVo.getStore().size()==0){
                //判断新增有无库位code,没有则退出提示您未做修改
              return ResultVo.error("您的依然没有任何库位信息！");

            }else if (storeCodes.size()!=0 && addLegalStoreVo.getStore().size()!=0){  //如果都有数据的情况下
                    //有库位则进行库位对比，没有则执行插入
                    for (String addStoreCode:addLegalStoreVo.getStore()){
                        if (!storeCodes.contains(addStoreCode)){
                            legalPersonRoomMapper.addLegalStoreRoom(addStoreCode,addLegalStoreVo.getLegalPersonCode());
                        }
                    }
                    //遍历原有的库位code，如果没有该code，就把原本数据中的法人code设置为空
                    for (String storeCode:storeCodes){
                        if (!addLegalStoreVo.getStore().contains(storeCode)){
                            legalPersonRoomMapper.removeLegalStoreRoom(storeCode);
                        }
                    }

            } else if (storeCodes.size()!=0&&addLegalStoreVo.getStore().size()==0) {//如果原有数据存在，新增没有
                //把原有数据进行删除即可
                for (String code:storeCodes){
                    legalPersonRoomMapper.removeLegalStoreRoom(code);
                }
            }else {
                //原有数据不存在，新增的数据的存在,直接执行插入即可
                for (String code:addLegalStoreVo.getStore()){
                    legalPersonRoomMapper.addLegalStoreRoom(code,addLegalStoreVo.getLegalPersonCode());
                }
            }
            return ResultVo.success("新增成功！");
            } catch (Exception e){
            e.printStackTrace();
            return ResultVo.error("修改失败");
        }

    }

    @Transactional(rollbackFor = Exception.class)
    public ResultVo addLegalStoreRoom2(AddLegalStoreVo addLegalStoreVo){
        try {
            //检查法人code
             Integer existLegal =  legalPersonMapper.getCountByCode(addLegalStoreVo.getLegalPersonCode());
             if (existLegal==0){
                 return ResultVo.error("您所选择的法人信息不存在！");
             }
            //获取该法人原有的库位code信息
            List<String> storeCodes = legalPersonRoomMapper.getStoreByLegalCode(addLegalStoreVo.getLegalPersonCode());
             if (storeCodes.size()==0){
                 //直接进行插入即可
                 legalPersonRoomMapper.addLegalStoreList(addLegalStoreVo);
             }else {
                 //1.把原本法人code下的库位法人清空
                 legalPersonRoomMapper.removeStoreLegal(addLegalStoreVo.getLegalPersonCode());
                 //2.执行插入
                 legalPersonRoomMapper.addLegalStoreList(addLegalStoreVo);
             }
            return ResultVo.success("添加成功！");
        }catch (Exception e){
            e.printStackTrace();
            //手工回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultVo.error("添加失败!");
        }
    }

    @Override
    public ResultVo queryStoreRoom(QueryStoreRoomVo queryStoreRoomVo) {
        List<StoreRoom> storeRoom = legalPersonRoomMapper.queryStoreRoom(queryStoreRoomVo);
        return ResultVo.success(storeRoom);
    }

    /**
     * 状态修改
     * @param updateStatusVo
     * @return
     */
    @Override
    public ResultVo updateStatus(UpdateStatusVo updateStatusVo) {
        legalPersonRoomMapper.updateStatus(updateStatusVo);
        return ResultVo.success("状态修改成功！");
    }

    /**
     * 根据法人拿到所有的库房库区库位信息
     * @param legalPersonCode
     * @return
     */
    @Override
    public ResultVo queryLegalStoreAll(String legalPersonCode) {
        //获取所有的库房信息
        List<StoreRoomVo> storeRoomVos = storeMapper.getStoreRoomByLegalPersonCode(legalPersonCode);
        //获取所有的库区信息
        List<StoreAreaVo> storeAreaVos = storeMapper.getStoreAreaByLegalPersonCode(legalPersonCode);
        //获取所有的库位信息
        List<CommonGetStoreVo> storeVos = storeMapper.getStoreByLegalPersonCode(legalPersonCode);

        GetAllStoreVo storeVoForWeb = new GetAllStoreVo();
        storeVoForWeb.setStoreRoomVos(storeRoomVos);
        storeVoForWeb.setStoreAreaVos(storeAreaVos);
        storeVoForWeb.setStoreVos(storeVos);
        return ResultVo.success(storeVoForWeb);
    }

    @Override
    public ResultVo queryAreaByRoomListAndAttribute(QueryAreaVo queryAreaVo) {
        List<StoreAreaVo> storeAreaVos = legalPersonRoomMapper.queryAreaByRoomListAndAttribute(queryAreaVo);
        return ResultVo.success(storeAreaVos);
    }

    @Override
    public ResultVo queryStoreByAreaCodes(QueryStoreVo queryStoreVo) {
        List<GetStoreForWebVo> storeVos = legalPersonRoomMapper.queryStoreByAreaCodes(queryStoreVo);
        //根据法人code筛选，只保留法人code为空或者为当前法人的数据
       /* List<GetStoreForWebVo> stores = new ArrayList<>();
        for (GetStoreForWebVo store:storeVos){
            if (store.getLegalCode()==null||store.getLegalCode().equals(queryStoreVo.getLegalPersonCode())){
                stores.add(store);
            }
        }*/
        return ResultVo.success(storeVos);
    }
}
